<?php

namespace my\wechat;

class Message
{
    private static $_wechat = null;

    public static $error = '';      //错误信息
    public static $touser = '@all';     //成员ID列表
    public static $agentid = '';     //应用ID
    public static $toparty = '';        //部门ID列表
    public static $totag = '';      //标签ID列表

    private static function wechat()
    {
        $c = [
            'appid' => c('wx_appid'),
            'appsecret' => c('wx_appsecret')
        ];

        if (empty(self::$agentid))
            self::$agentid = c('send_message_agentid');

        if (self::$_wechat == null)
            self::$_wechat = new QyWeChat($c);

        return self::$_wechat;
    }

    /**
     * 微信发送消息
     * @param $type
     * @param $data
     * @return array|bool
     * @author wb <pithyone@vip.qq.com>
     */
    public static function send($type, $data)
    {
        if ($type == 'text')
            $message = self::_text($data);
        elseif ($type == 'image')
            $message = self::_image($data);
        elseif ($type == 'voice')
            $message = self::_voice($data);
        elseif ($type == 'file')
            $message = self::_file($data);
        elseif ($type == 'news')
            $message = self::_news($data);
        else {
            self::$error = '请选择发送消息的类型';
            return false;
        }

        $ret = self::wechat()->sendMessage($message);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 上传多媒体文件
     * @param $type
     * @param $data
     * @return array|bool
     * @author wb <pithyone@vip.qq.com>
     */
    public static function upload($type, $data)
    {
        $ret = self::wechat()->uploadMedia($data, $type);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 文本消息数据格式
     * @param $content
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _text($content)
    {
        $base_message = self::_message();
        $text = [
            "msgtype" => "text",
            "text" => [
                "content" => $content
            ]
        ];

        return array_merge($base_message, $text);
    }

    /**
     * 图片消息数据格式
     * @param $media_id
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _image($media_id)
    {
        $base_message = self::_message();
        $image = [
            "msgtype" => "image",
            "image" => [
                "media_id" => $media_id
            ]
        ];

        return array_merge($base_message, $image);
    }

    /**
     * 语音消息数据格式
     * @param $media_id
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _voice($media_id)
    {
        $base_message = self::_message();
        $voice = [
            "msgtype" => "voice",
            "voice" => [
                "media_id" => $media_id
            ]
        ];

        return array_merge($base_message, $voice);
    }

    /**
     * 文件消息数据格式
     * @param $media_id
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _file($media_id)
    {
        $base_message = self::_message();
        $file = [
            "msgtype" => "file",
            "file" => [
                "media_id" => $media_id
            ]
        ];

        return array_merge($base_message, $file);
    }

    /**
     * 图文消息数据格式
     * @param $article
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _news($article)
    {
        $base_message = self::_message();
        $news = [
            "msgtype" => "news",
            "news" => [
                "articles" => $article
            ]
        ];

        return array_merge($base_message, $news);
    }

    /**
     * 基本消息数据格式
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _message()
    {
        $ret = [
            "touser" => self::$touser,
            "agentid" => self::$agentid,
            "safe" => "0"
        ];

        if (!empty(self::$toparty))
            $ret['toparty'] = self::$toparty;

        if (!empty(self::$totag))
            $ret['totag'] = self::$totag;

        return $ret;
    }
}